<!--
  * 任务列表
-->
<template>
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="任务名称" class="smart-query-form-itme" name="name">
        <a-input style="width: 300px" v-model:value="queryForm.taskName" placeholder="请输入任务名称" />
      </a-form-item>

      <a-form-item label="任务状态" name="state" class="smart-query-form-item">
        <DictSelectType :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="DICT_TYPE_ENUM.AS_STATE.value" v-model:value="queryForm.state" width="120px" />
      </a-form-item>
      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button type="primary" @click="ajaxQuery">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="add()" v-privilege="'as:task:add'" type="primary" size="small">
          <template #icon>
            <PlusOutlined />
          </template>
          新建任务
        </a-button>
      </div>
    </a-row>
    <a-table
      :scroll="{ x: 1300 }"
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="id"
      :pagination="false"
      :loading="tableLoading"
      bordered
    >
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'taskName'">
          <a @click="detail(record.id,record.taskName)">{{ text }}</a>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button v-if=" record.state===1 " @click="doSeg(record.id)" v-privilege="'as:task:seg'" type="link">分词</a-button>
            <a-button v-if=" record.state===2 " @click="doFix(record.id)" v-privilege="'as:task:fix'" type="link">修复补齐</a-button>
            <a-button v-if=" record.state===3 " @click="doMatch(record.id)" v-privilege="'as:task:match'" type="link">匹配</a-button>
            <a-button v-if=" record.state===4 " @click="doUpload(record.id)" v-privilege="'as:task:match'" type="link">导出</a-button>
            <a-button v-if=" record.state===0 " @click="update(record)" v-privilege="'as:task:edit'" type="link">编辑</a-button>
            <a-button v-if=" record.state===0 " @click="confirmDelete(record.id)" danger v-privilege="'as:task:delete'" type="link">删除</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageIndex"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>
    <TaskOperate ref="operateRef" @refresh="ajaxQuery" />
    <UpdateFormModal ref="updateFormModal" @refresh="ajaxQuery" />
  </a-card>
</template>
<script setup lang="ts">
  import { reactive, ref, onMounted,computed } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { asApi } from '/@/api/addrstandard/as-api'
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { useRouter } from 'vue-router';
  import TaskOperate from './components/task-operate-modal.vue';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
  import DictSelectType from '/@/components/cache/dict-select-type.vue'

  // --------------------------- 企业表格 列 ---------------------------

  const columns = ref([
    {
      title: '任务名称',
      dataIndex: 'taskName',
      minWidth: 180,
      ellipsis: true,
    },
    {
      title: '城市',
      dataIndex: 'cityCode',
      minWidth: 100,
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      minWidth: 150,
      ellipsis: true,
    },
    {
      title: '任务地址总数量',
      dataIndex: 'tastCnt',
      minWidth: 100,
    },
    {
      title: '有效地址数量',
      dataIndex: 'validCnt',
      minWidth: 100,
    },
    {
      title: '外部任务ID',
      dataIndex: 'outTaskId',
      minWidth: 100,
    },
    {
      title: '创建时间',
      minWidth: 100,
      dataIndex: 'createTime',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 150,
    },
  ]);

  // --------------------------- 查询 ---------------------------

  const queryFormState = {
    taskName: undefined,
    state: undefined,
    pageIndex: 1,
    pageSize: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      let responseModel = await asApi.searchPage(queryForm);
      total.value = responseModel.data.total;
      tableData.value = responseModel.data.data;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // --------------------------- 删除 ---------------------------

  function confirmDelete(taskId) {
    Modal.confirm({
      title: '确定要删除吗？',
      content: '删除后，该信息将不可恢复',
      okText: '删除',
      okType: 'danger',
      onOk() {
        del(taskId);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  async function del(taskId) {
    try {
      SmartLoading.show();
      await asApi.deleteAs(taskId);
      message.success('删除成功');
      ajaxQuery();
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      SmartLoading.hide();
    }
  }

  // --------------------------- 增加、修改、详情 ---------------------------

    const updateFormModal =ref();

   async function doSeg(tsakId){
    let res=await asApi.doSeg(tsakId);
    ajaxQuery();
    message.success(res.message);
   }

   async function doFix(tsakId){
    let res=await asApi.doFix(tsakId);
    ajaxQuery();
    message.success(res.message);
   }

   async function doMatch(tsakId){
    let res=await asApi.doMatch(tsakId);
    ajaxQuery();
    message.success(res.message);
    }

    function doUpload(taskId){
      message.info("导出匹配信息")
    }

  let router = useRouter();
  const operateRef = ref();
  function add() {
    operateRef.value.showModal();
  }

  function update(record) {
    operateRef.value.showModal(record);
  }

  function detail(taskId,taskName) {
    router.push({ path: '/as/taskifno/detail', query: { taskId: taskId,taskName:taskName } });
  }

  onMounted(ajaxQuery);
</script>
